<?php
namespace app\admin\controller;
use think\Db;

class Adset extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $param = input();
        // $param['page'] = intval($param['page']) <1 ? 1 : $param['page'];
        // $param['limit'] = intval($param['limit']) <1 ? $this->_pagesize : $param['limit'];
        $where=[];

        $order='id desc';
        //开屏
        $where['adtype'] = 3;
        $res = model('Ad')->listAll($where,$order);
        $this->assign('kaiping',$res['list'][0]);
        
        //详情
        $where['adtype'] = 4;
        $res = model('Ad')->listAll($where,$order);
        $this->assign('xiangqing',$res['list'][0]);
        
        //个人中心
        $where['adtype'] = 5;
        $res = model('Ad')->listAll($where,$order);
        $this->assign('myad',$res['list'][0]);
        
        //赛事列表
        $where['adtype'] = 0;
        $order='isxiajia desc, type_id asc, saishishunxu asc, id desc';
        
        $res = model('Ad')->listAll($where,$order);
        $this->assign('saishilist',$res['list']);
        
        //轮播列表
        $whereOr = ['adtype' => 1, 'from' => 1];
        $order='lunboshunxu asc, id desc';
        $res = model('Ad')->listAllOr($whereOr,$order);
        $this->assign('lunbolist',$res['list']);
        
        //图片列表
        $where['adtype'] = 2;
        $order='istuijian asc, id desc';
        $res = model('Ad')->listAll($where,$order);
        $this->assign('piclist',$res['list']); 

        $this->assign('title','广告管理');
        return $this->fetch('admin@adset/index');
    }
    
    public function updateShunxu() {
        
        $id = input('id');
        $shunxu = input('shunxu');
        $shunxutype = input('shunxutype');
        
        $res = model('Ad')->updateData(['id' => $id], [$shunxutype => $shunxu]);
        if($res['code']>1){
            return $this->error($res['msg']);
        }
        return $this->success($res['msg']);
    }
    
    public function saishiinfo()
    {
        if (Request()->isPost()) {
            $param = input('post.');
            $res = model('Ad')->saveData($param);
            if($res['code']>1){
                return $this->error($res['msg']);
            }
            return $this->success($res['msg']);
        }

        $id = input('id');
        $where=[];
        $where['id'] = ['eq',$id];
        $res = model('Ad')->infoData($where);

        $zhibo_id = input('zhibo_id',0,'intval');
        $type_id = input('type_id',0,'intval');
        $alias_name = input('alias_name','','trim');
        if(!empty($alias_name)){
            $gameInfo = \db('zhibo_sort_import')->where('name',$alias_name)->find();
        }
        if(!empty($zhibo_id) && !empty($type_id) ){
            $res['info'] =[
                'zhibo_id'=>$zhibo_id,
                'type_id'=>$type_id,
            ];

            if(!empty($gameInfo)){
                $res['info']['game_id']=$gameInfo['id'];
            }
        }
        $this->assign('info',$res['info']);
        $this->assign('title','赛事广告管理');
        return $this->fetch('admin@adset/saishiinfo');
    }
    
    public function lunboinfo()
    {
        if (Request()->isPost()) {
            $param = input('post.');
            $res = model('Ad')->saveData($param);
            if($res['code']>1){
                return $this->error($res['msg']);
            }
            return $this->success($res['msg']);
        }

        $id = input('id');
        $where=[];
        $where['id'] = ['eq',$id];
        $res = model('Ad')->infoData($where);
        
        $this->assign('info',$res['info']);
        $this->assign('title','轮播广告管理');
        return $this->fetch('admin@adset/lunboinfo');
    }
    
    public function liebiaoinfo()
    {
        if (Request()->isPost()) {
            $param = input('post.');
            $res = model('Ad')->saveData($param);
            if($res['code']>1){
                return $this->error($res['msg']);
            }
            return $this->success($res['msg']);
        }

        $id = input('id');
        $where=[];
        $where['id'] = ['eq',$id];
        $res = model('Ad')->infoData($where);
        
        $this->assign('info',$res['info']);
        $this->assign('title','列表广告管理');
        return $this->fetch('admin@adset/liebiaoinfo');
    }
    
    public function kaipinginfo()
    {
        if (Request()->isPost()) {
            $param = input('post.');
            $res = model('Ad')->saveData($param);
            if($res['code']>1){
                return $this->error($res['msg']);
            }
            return $this->success($res['msg']);
        }

        $id = input('id');
        $where=[];
        $where['id'] = ['eq',$id];
        $res = model('Ad')->infoData($where);
        
        $this->assign('info',$res['info']);
        $this->assign('title','开屏广告管理');
        return $this->fetch('admin@adset/kaipinginfo');
    }

    public function xiangqinginfo()
    {
        if (Request()->isPost()) {
            $param = input('post.');
            $res = model('Ad')->saveData($param);
            if($res['code']>1){
                return $this->error($res['msg']);
            }
            return $this->success($res['msg']);
        }

        $id = input('id');
        $where=[];
        $where['id'] = ['eq',$id];
        $res = model('Ad')->infoData($where);
        
        $this->assign('info',$res['info']);
        $this->assign('title','详情广告管理');
        return $this->fetch('admin@adset/xiangqinginfo');
    }

    public function myadinfo()
    {
        if (Request()->isPost()) {
            $param = input('post.');
            $res = model('Ad')->saveData($param);
            if($res['code']>1){
                return $this->error($res['msg']);
            }
            return $this->success($res['msg']);
        }

        $id = input('id');
        $where=[];
        $where['id'] = ['eq',$id];
        $res = model('Ad')->infoData($where);
        
        $this->assign('info',$res['info']);
        $this->assign('title','个人中心广告管理');
        return $this->fetch('admin@adset/myadinfo');
    }

    public function changeStatus() {
        $param = input();
        $res = \db('ad')->where('id',$param['id'])->update(['status' => $param['status']]);
        if($res) {
            return $this->success();
        } else {
            return $this->error();
        }
    }

    public function changeFrom() {
        $param = input();
        $res = \db('ad')->where('id',$param['id'])->update(['from' => $param['from']]);
        if($res) {
            return $this->success();
        } else {
            return $this->error();
        }
    }
    
    public function del()
    {
        $param = input();
        $ids = $param['ids'];
        $all = $param['all'];

        if(!empty($ids)){
            $where=[];
            $where['id'] = ['in',$ids];
            if($all==1){
                $where['id'] = ['gt',0];
            }
            $res = model('Ad')->delData($where);
            if($res['code']>1){
                return $this->error($res['msg']);
            }
            return $this->success($res['msg']);
        }
        return $this->error('参数错误');
    }



}
